"""
环信 配置读取
"""
import os
import configparser


class HxConfigParser:
    def __init__(self):
        self.config_path = os.path.join(os.path.dirname(__file__), "hx.ini")
        self.conf = configparser.ConfigParser()
        self.conf.read(self.config_path, encoding="utf-8")

    def get_url(self):
        """
        请求地址
        """
        app_key = self.get_app_key()
        base_url = self.conf.get("hx", "hx_url")
        sub_url = "/".join(app_key.split("#"))
        return f"{base_url}/{sub_url}"

    def get_app_key(self):
        """
        app_key
        """
        return self.conf.get("hx", "app_key")

    def get_app_token(self):
        """
        管理员token
        """
        return self.conf.get("token", "app_token")

    def update_app_token(self, token):
        """
        更新管理员token
        """
        self.conf.set("token", "app_token", token)
        self.conf.write(open(self.config_path, "w+", encoding="utf-8"))

    def get_client_id(self):
        """
        获取client_id
        """
        return self.conf.get("hx", "client_id")

    def get_client_secret(self):
        """
        获取 client_secret
        """
        return self.conf.get("hx", "client_secret")


if __name__ == '__main__':
    result = HxConfigParser().update_app_token("111")
    print(result)
